/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment3;

/**
 *
 * @author Çağatay Kızıldağ
 */
public class Rasyonel2
{

    int a,b;

    public Rasyonel2()
    {
        a = 0;
        b = 1;
    }
    
    public Rasyonel2(int x , int y)
    {
        a = x;
        b = y;
        this.simplify();
    }

    public double Value()
    {
      double value =(double) a/b;
      return value;
    }

    public String ToString()
    {
      return a+"/"+b;
    }

    public void setNumber1(int number1)
    {
        a = number1;
        this.simplify();
    }
    public void setNumber2(int number2)
    {
        b = number2;
        this.simplify();
    }

    public int getNumber1()
    {
       return a;
    }
    
    public int getNumber2()
    {
       return b;
    }
    
    private void simplify()
    {
       int GCD = getGCD(a,b);
       a = a/GCD;
       b = b/GCD;
    }

    public Rasyonel2 add(Rasyonel2 secondNumber)
    {
        int sum1 = (a*secondNumber.getNumber2() + b*secondNumber.getNumber1());
        int sum2 = b*secondNumber.getNumber2();
        Rasyonel2 sum = new Rasyonel2(sum1,sum2);
        return sum;
    }

    private static int getGCD(int number1,int number2)
    {
       int GCD=1;
        while (number1!=number2)
        {
            if (number1>number2)
                {
                number1 = number1-number2;
                }
            else
                {
                number2 = number2-number1;
                }
        }
       GCD = number1;
        return GCD;
    }
}
